<?

namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use think\Db;

class Sale extends Command
{
    protected function configure(){
        $this->setName('Sale')->setDescription("Test auto exec");
    }

    protected function execute(Input $input, Output $output){
        while(true):
            //预售到进行中
            Db::startTrans();
            try{
                $time=time();
                $ids=db('task')->where("end_time <=$time and status =1 ")->column('id');
                if($ids){
                    db('task')->where("id in (".implode(',',$ids).")")->update(['status'=>3,'complete_time'=>time()]);
                    $output->writeln("task time out:".json_encode($ids));
                }
                Db::commit();//事务提交
            } catch (\Exception $e) {
                error_log($e->getMessage());
                Db::rollback();
            }

            sleep(5);
            endwhile;
    }


}
